/**
* @file debug.c
* @brief debug application
* @details debug interface
* @author huangbin
* @version V1.0.0
* @date 2025-04-20
* @copyright All documents are copyrighted by Shenzhen Xinlongwei Technology Co., LTD.
*            They may not be used for commercial purposes without permission
*/

#include "inc.h"

#if defined(DEBUG_LOG_TO_LCD) && DEBUG_LOG_TO_LCD
#   define DEBUG_SEND_STRING(s,len)                                             \
    do {                                                                        \
        uint8_t *pchString = (uint8_t *)s;                                      \
        while(len--) {                                                          \
            disp_adapter0_putchar(*pchString++);                                \
        }                                                                       \
    } while(0);
#else
#   define DEBUG_SEND_STRING(s,len) uart1_send_loop(s,len)
#endif

#ifdef DEBUG
void DebugPrintf(char* str,...)
{
    static char txt[DEBUG_BUFFER_MAX];
    uint16_t len;
    __va_list args;

    va_start(args,str);
    len = vsnprintf((char *)txt,DEBUG_BUFFER_MAX,(const char *)str,args);
    va_end(args);
    DEBUG_SEND_STRING((const uint8_t*)txt,len);
}

void DebugWriteHex(uint8_t *buf,uint32_t len)
{
    uint32_t i,block,residual;
    uint8_t buffer[16];
    block = len >> 4;
    residual = len & 0xF;
    for(i = 0; i < block;i++) {
        memcpy(buffer,buf+i*16,16);
        DebugPrintf("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x \r\n",
                    buffer[0],buffer[1],buffer[2],buffer[3],buffer[4],buffer[5],buffer[6],buffer[7],
                    buffer[8],buffer[9],buffer[10],buffer[11],buffer[12],buffer[13],buffer[14],buffer[15]);
    }
    if(residual) {
        memcpy(buffer,buf+block*16,residual);
        for(i = 0; i < residual;i++) {
            DebugPrintf("%02x ",buffer[i]);
        }
        DebugPrintf("\r\n");
    }
}
#endif
#if defined(__MICROLIB)
void __aeabi_assert(const char *chCond, const char *chLine, int wErrCode) 
{
    (void)chCond;
    (void)chLine;
    (void)wErrCode;
    
    while(1) {
    }
}
#else
#if (__ARMCC_VERSION >= 6010050)
    __asm(".global __use_no_semihosting");
    __asm(".global __ARM_use_no_argv");
#else
    #pragma import(__use_no_semihosting)

struct __FILE
{
    int handle;
    /* Whatever you require here. If the only file you are using is */
    /* standard output using printf() for debugging, no file handling */
    /* is required. */
};

#endif

#if !defined(RTE_CMSIS_Compiler_STDOUT)
typedef int FILEHANDLE;
FILEHANDLE _sys_open(const char *name,int openmode)
{
 return 0;
}
int _sys_close(FILEHANDLE fh)
{
    return 0;
}
int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)
{
    return 0;
}
int _sys_read(FILEHANDLE fh, unsigned char*buf, unsigned len, int mode)
{
    return 0;
}

int _sys_istty(FILEHANDLE fh)
{
    return 0;
}
int _sys_seek(FILEHANDLE fh, long pos)
{
    return 0;
}

int _sys_ensure(FILEHANDLE fh)
{
    return 0;
}

long _sys_flen(FILEHANDLE fh)
{
    return 0;
}

int _sys_tmpnam(char *name, int fileno, unsigned maxlength)
{
    return 0;
}


int remove(const char *filename)
{
    return 0;
}
char *_sys_command_string(char *cmd, int len)
{
 return NULL;
}
#endif

void _sys_exit(int status)
{
    while(1) __NOP();
}

void _ttywrch(int ch)
{

}

extern void HardFault_Handler(void);
void __aeabi_assert(const char *chCond, const char *chLine, int wErrCode) 
{
    (void)chCond;
    (void)chLine;
    (void)wErrCode;
    DebugPrintf("arm-2d assert: %s,%s,%d\r\n",chCond,chLine,wErrCode);
    while(1) {
        HardFault_Handler();
    }
}

//int fputc(int ch,FILE *p)
//{
//	return 0;
//}
//int64_t clock(void)
//{
//    return 0;
//}
#endif
//////////////////////////////File end/////////////////////////////////////

